package indiarockks;

import com.google.wave.api.*;

import java.util.logging.Logger;

@SuppressWarnings("serial")
public class IndiarockksServlet extends AbstractRobotServlet {
	// Log for testing purposes
	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(IndiarockksServlet.class
			.getName());
	public static final String AMOUNT_FIELD = "AMOUNT_FIELD";
	public static final String NAME_FIELD = "NAME_FIELD";
	public static final String APPROVE_BUTTON = "APPROVE_BUTTON";
	public static final String REJECT_BUTTON = "REJECT_BUTTON";
	@Override
	public void processEvents(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet();
		if (bundle.wasSelfAdded()) {

			
			
		}
		for (Event e : bundle.getEvents()) {

			if (e.getType() == EventType.WAVELET_SELF_ADDED) {

				Wavelet wavelt = e.getWavelet();
				wavelt.addParticipant("saurabh.sgsits@googlewave.com");

			}

			if (e.getType() == EventType.WAVELET_PARTICIPANTS_CHANGED) {

				Blip initBlip = wavelet.appendBlip();
				TextView firstTextView = initBlip.getDocument();

				Wavelet wavelt = e.getWavelet();

				for (String participant : e.getAddedParticipants())
					firstTextView.append("Hi " + participant + " welcome to "
							+ wavelt.getTitle() + " !!! You were invited by "+ wavelt.getCreator()+".");

			}

			// Actions take place when blip is submitted
			if (e.getType() == EventType.BLIP_SUBMITTED) {
				Blip blip = wavelet.appendBlip();
				TextView textView = blip.getDocument();
				textView.appendMarkup("<p><b>Welcome to the First Wave Bank</b></p>");
				textView.append("\nDescribe what you want to borrow to in the wave.  \n");
				textView.append("Then fill out the form for your information\n");
				FormView form = textView.getFormView();
				form.append(new FormElement(ElementType.LABEL, "labelname",
						"Your name"));
				form.append(new FormElement(ElementType.INPUT, NAME_FIELD));
				textView.append("\n");
				form.append(new FormElement(ElementType.LABEL, "labelamount",
						"Borrow amount in $"));
				form.append(new FormElement(ElementType.INPUT, AMOUNT_FIELD));
				form.append(new FormElement(ElementType.BUTTON, "submit", "Apply"));
//				Blip blip = e.getBlip();
//				// Only process standard blips (i.e. not title blip)
//				if (blip.getBlipId().equals(blip.getWavelet().getRootBlipId()) == false) {
//					doSomeThing(blip);
//				}

			}
		}
	}
	


}
